﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using QuanLyBanHang.OBJECT;

namespace QuanLyBanHang.DAL
{
    public static class DalUsers
    {

        public static ObjUsers user;

        public static DataTable DangNhap(string TK, string MK)
        {
            try
            {
                DataHelper.Open();
                SqlConnection conn = DataHelper.con;
                SqlCommand cmd = new SqlCommand(@"select TaiKhoan,MatKhau,Ten,Users.MaNV,TenQuyen from NhanVien inner join
                                                Users on NhanVien.MaNV = Users.MaNV inner join Quyen on 
                                                Users.MaQuyen = Quyen.MaQuyen where TaiKhoan = @TK and MatKhau = @MK", conn);
                cmd.Parameters.AddWithValue("TK", TK);
                cmd.Parameters.AddWithValue("MK", MK);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static int DoiMatKhau(string TK, string MKCu, string MKMoi)
        {
            try
            {
                SqlParameter[] para = new SqlParameter[3];
                string[] names = { "TaiKhoan", "MatKhauCu", "MatKhauMoi" };
                object[] values = { TK, MKCu, MKMoi };
                for (int i = 0; i < para.Length; i++)
                {
                    para[i] = new SqlParameter(names[i], values[i]);
                }
               return DataHelper.Update(para, "spDoiMatKhauUsers");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static DataTable XemThongTin(string TK)
        {
            try
            {
                DataHelper.Open();
                SqlConnection conn = DataHelper.con;
                SqlCommand cmd = new SqlCommand(@"select Ten,GioiTinh,NgaySinh,SoDienThoai,CMND,DiaChi,
                                                TaiKhoan,Email,TenQuyen from NhanVien inner join
                                                Users on NhanVien.MaNV = Users.MaNV inner join Quyen on 
                                                Users.MaQuyen = Quyen.MaQuyen where TaiKhoan = @TK", conn);
                cmd.Parameters.AddWithValue("TK", TK);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static int Insert(ObjUsers user)
        {
            try
            {
                SqlParameter[] para = new SqlParameter[5];
                string[] names = {"TaiKhoan","MatKhau","Email","MaQuyen","MaNV" };
                object[] values = {user.TaiKhoan,user.MatKhau,user.Email,user.MaQuyen,user.MaNV};
                for (int i = 0; i < para.Length; i++)
                {
                    para[i] = new SqlParameter(names[i], values[i]);
                }
                return DataHelper.Update(para, "spInsertUsers");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static int Update(ObjUsers user)
        {
            try
            {
                SqlParameter[] para = new SqlParameter[5];
                string[] names = { "TaiKhoan", "MatKhau", "Email", "MaQuyen", "MaNV" };
                object[] values = { user.TaiKhoan, user.MatKhau, user.Email, user.MaQuyen, user.MaNV };
                for (int i = 0; i < para.Length; i++)
                {
                    para[i] = new SqlParameter(names[i], values[i]);
                }
                return DataHelper.Update(para, "spUpdateUsers");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static int Delete(string TaiKhoan)
        {
            try
            {
                SqlParameter[] para = { new SqlParameter("TaiKhoan",TaiKhoan)};
                return DataHelper.Update(para, "spDeleteUsers");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}
